package com.hyc.myvideo.ui.view;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.hyc.myvideo.R;
import com.hyc.myvideo.ui.base.BaseDialogFragment;
import com.hyc.myvideo.widget.ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author 弹框提示审批
 * @version 1.0
 * @created 2017/8/11
 */

public class DialogApproval extends BaseDialogFragment {
    //审批内容
    @BindView(R.id.e_content)
    ClearEditText editText;
    //审批内容长度
    @BindView(R.id.t_content_number)
    TextView textViewNumber;
    /**确定*/
    @BindView(R.id.b_submit)
    Button buttonDetermine;
    private SubmitInterface submitInterface = null;
    public DialogApproval(SubmitInterface smInterface) {
        this.submitInterface = smInterface;
    }

    @Override
    public int getContentViewId() {
        return R.layout.dialog_approval;
    }

    @Override
    public int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected void initView(){
        textViewNumber.setText(0 + "/" + 100);
        editText.addTextChangedListener(textWatcher);
        editText.setText("");
        setButtonSubmit(false);
    }


    /**
     * 字数监听
     */
    TextWatcher textWatcher = new TextWatcher() {
        @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
        @Override public void afterTextChanged(Editable s) {
            int num = s.toString().length();
            textViewNumber.setText(num + "/" + (100 - num));
            if(num >= 2){
                setButtonSubmit(true);
            }else {
                setButtonSubmit(false);
            }
        }
    };

    /***
     * 设置提交按钮状态
     * @param isClickable
     */
    private void setButtonSubmit(boolean isClickable){
        buttonDetermine.setClickable(isClickable);
        if(isClickable){
            buttonDetermine.setBackgroundResource(R.drawable.button_shape_35_0);
        }else{
            buttonDetermine.setBackgroundResource(R.drawable.button_shape_35_1);
        }
    }


    @OnClick({R.id.b_submit})
    public void onClick(View v) {
        switch (v.getId()){
            //提交
            case R.id.b_submit:
                hideDialog();
                submitInterface.onSubmit(editText.getText().toString().replaceAll(" ", "").trim());
                break;

        }
    }

    public interface SubmitInterface{
        //提交
        void onSubmit(String sr);
    }


}
